var outerLayout;
var innerLayout;
var scriptEditor;

$().ready(function() {
	outerLayout = $("body").layout({
		defaults: {
			fxName:               "slide",
			fxSpeed:               "slow",
			spacing_closed:        14
		},
		north: {
			fxName: "none",
			resizable: false,
			spacing_open: 0
		}
	});
	innerLayout = $("div.ui-layout-center").layout({
		center__paneSelector:	".inner-center",
		south__paneSelector: 	".inner-south",
		south__minSize:			200,
		south__initClosed:		true,
		south__spacing_open:	0,
		south__spacing_closed:	0
	});
	$('#runScriptButton').click(function() { 
		runScript();
	});
});


function openScriptView() {
	$('#inner-center').html("<textarea id='input-area'></textarea>");
	scriptEditor = CodeMirror.fromTextArea('input-area', {
	  parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
	  stylesheet: "css/jscolors.css",
	  lineNumbers: true,
	  autoMatchParens: true,
	  indentUnit: 4
	});
	
	innerLayout.open("south");
	$("#runScriptBox").show();
}
	
function runScript() {	
	$.post(getURI()+"/script", { "script": scriptEditor.getCode() },
	  function(data){
		document.getElementById("output-area").value = data;
	  }, "text");
}

function openJNDIView() {
	$("#runScriptBox").hide();
	innerLayout.close("south");
	$('#inner-center').xslt(getURI()+"/jndi", getURI()+"/xsl/jndi.xsl", "POST");
}

function openJMXView() {
	$("#runScriptBox").hide();
	innerLayout.close("south");
	$('#inner-center').xslt(getURI()+"/log4j", getURI()+"/xsl/log4j.xsl", "POST");
}

function openJMXView() {
	$("#runScriptBox").hide();
	innerLayout.close("south");
	$('#inner-center').xslt(getURI()+"/jmx", getURI()+"/xsl/jmx.xsl", "POST");
}

function openContextView(context) {
	$("#runScriptBox").hide();
	innerLayout.close("south");
	$('#inner-center').xslt(getURI()+"/context/"+context, getURI()+"/xsl/context.xsl", "POST");
}

function getURI() {
	var uri = document.location.protocol+"//"+document.location.host+document.location.pathname;
	return uri;
}
